﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using $ext_safeprojectname$.Controllers;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

namespace $safeprojectname$.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    [Route("/api/app/[controller]/[action]")]
    [TypeFilter(typeof(RoleAttribute), Arguments = new object[] { "data", "add" })]
    public class UploadController : IBaseController
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dbContext"></param>
        public UploadController(I$ext_safeprojectname$DbContext dbContext) : base(dbContext)
        {

        }

        /// <summary>
        /// 富文本上传图片 控件
        /// </summary>
        /// <param name="typeid"></param>
        /// <param name="objid"></param>
        /// <returns></returns>
        [HttpPost]
        public WangEditorUpload UploadImage(int typeid=0, int objid=0)
        {
            var one = new WangEditorUpload();
            one.errno = 1;
            if (base.Request.Form != null)
            {
                if (base.Request.Form.Files != null)
                {
                    if (base.Request.Form.Files.Count > 0)
                    {
                        one.errno = 0;
                        one.data = new List<WangEditorUploadItem>();
                        for (var k = 0; k < base.Request.Form.Files.Count; k++)
                        {
                            var finput = base.Request.Form.Files[k];
                            //这里还要进行文件后缀的过滤
                            var item = new WangEditorUploadItem();
                            var extension = System.IO.Path.GetExtension(finput.FileName);
                            var filename = $"{finput.FileName}{k}{DateTimeOffset.Now.ToUnixTimeMilliseconds()}".ToMd5Lower();
                            var savepath = $"upload/{typeid}/{objid}/{filename}.{extension}";
                            var path = System.IO.Path.GetDirectoryName(savepath);
                            if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); }
                            item.alt = finput.FileName;
                            item.href = $"/{savepath}";
                            item.url = $"/{savepath}";
                            using var image = Image.Load<Rgba32>(finput.OpenReadStream());
                            image.Mutate(x => x.Resize(0, 180));
                            image.Save(savepath);
                            item.url = $"/{savepath}";
                            finput.OpenReadStream().Dispose();
                            item.size = finput.Length;
                            one.data.Add(item);
                        }
                    }
                }
            }
            return one;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private bool IsImageExtension(string filename)
        {
            if (filename.ToLower().EndsWith(".jpeg"))
            {
                return true;
            }
            if (filename.ToLower().EndsWith(".jpg"))
            {
                return true;
            }
            if (filename.ToLower().EndsWith(".bmp"))
            {
                return true;
            }
            if (filename.ToLower().EndsWith(".gif"))
            {
                return true;
            }
            if (filename.ToLower().EndsWith(".webp"))
            {
                return true;
            }
            if (filename.ToLower().EndsWith(".png"))
            {
                return true;
            }

            return false;
        }

    }

    /// <summary>
    /// 
    /// </summary>
    public class WangEditorUpload
    {

        /// <summary>
        /// 
        /// </summary>
        public int errno { get; set; } = 0;
        /// <summary>
        /// 
        /// </summary>
        public List<WangEditorUploadItem> data { get; set; }

    }
    /// <summary>
    /// 
    /// </summary>
    public class WangEditorUploadItem
    {

        /// <summary>
        /// 
        /// </summary>
        public string url { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string alt { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public string href { get; set; }

        /// <summary>
        /// 资源大小
        /// </summary>
        public long size { get; set; }
    }
}
